package com.styytech.dadabao.api.requestresult;

import android.content.Context;

import com.styytech.dadabao.api.operations.UserOperations;
import com.styytech.dadabao.api.response.AbstractResponseResult;
import com.styytech.dadabao.constant.ConstantsCommon;
import com.styytech.dadabao.utils.SpUser;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by user on 2015/8/3.
 */
public class LoginResult extends RequestResult implements UserOperations {
    private Context context;

    /**
     * 构造函数
     *
     * @param httpResponseResult
     */
    public LoginResult(AbstractResponseResult httpResponseResult) {
        super(httpResponseResult);
    }

    public LoginResult(Context context, AbstractResponseResult httpResponseResult) {
        super(httpResponseResult);
        this.context = context;
    }

    @Override
    public void doBusiness() {
        super.doBusiness();
        if (isSuccess()) {
            // 处理成功的逻辑
            dataParse((JSONArray) super.getRows());
        }
    }

    private void dataParse(JSONArray jsonArray) {
        String token = null;
        //实名认证
        int idCardVStatus = 0;
        //支付密码
        int payPasswordVStatus = 0;
        //绑定银行卡
        int cardVStatus = 0;
        //绑定手机
        int mobileVStatus = 0;
        String phone;
        String realName;
        if (jsonArray != null && !jsonArray.isNull(0)) {
            try {
                JSONObject json = jsonArray.getJSONObject(0);
                token = json.has("token") ? json.getString("token") : null;
                idCardVStatus = json.has("idCardVStatus") ? json.getInt("idCardVStatus") : 0;
                payPasswordVStatus = json.has("payPasswordVStatus") ? json.getInt("payPasswordVStatus") : 0;
                cardVStatus = json.has("cardVStatus") ? json.getInt("cardVStatus") : 0;
                mobileVStatus = json.has("mobileVStatus") ? json.getInt("mobileVStatus") : 0;
                phone = json.has("mobile") ? json.getString("mobile") : "";
                realName = json.has("realName") ? json.getString("realName") : null;
                SpUser.setUserType(context,json.has("memberType") ? json.getInt("memberType") : 1);
                SpUser.setUserJobNum(context,json.has("memberNo")? json.getString("memberNo") : "");
                setTokens(token, idCardVStatus, payPasswordVStatus, cardVStatus, mobileVStatus, phone, realName);
            } catch (JSONException e) {
                e.printStackTrace();
                if (httpResponseResult != null) {
                    httpResponseResult.executeError("登录失败",
                            AbstractResponseResult.RESPONSE_DATA_FORMAT_ERROR);
                }
            }

        }
        if (httpResponseResult != null) {
            httpResponseResult.executeSuccess(token);
        }
    }

    /**
     * 存储token
     *
     * @param token
     */
    @Override
    public void setTokens(String token, int idCardVStatus, int payPasswordVStatus, int cardVStatus, int mobileVStatus, String phone, String realName) {
        SpUser.setUserToken(context, token);
        SpUser.setIdCardVStatus(context, idCardVStatus);
        SpUser.setBuyPasswordStatus(context, payPasswordVStatus);
        SpUser.setBankCardVStatus(context, cardVStatus);
        SpUser.setMobileNumer(context, phone);
        SpUser.setMobileVStatus(context, mobileVStatus);
        SpUser.setUserInfo(context, ConstantsCommon.USER_REALNAME, realName);

    }

    @Override
    public void setToken(String paramString) {

    }
}
